<?php

session_start();
if(!array_key_exists("user", $_SESSION) || $_SESSION['account'] != 2){
	header("Location: ../common/redirect.php");
	exit;
}

if(!array_key_exists("day", $_GET)){
	header("Location: schedule.php?action=fail");
	exit;
}

include_once("../class/db.php");
$db = new db();

if(array_key_exists("id", $_POST)){
	$id = $_POST['id'];
	$end_date1 = explode(" ",  preg_replace("/[^a-zA-Z0-9_\-:\.\s]/", "", $_POST['end_date']));
	$end_date2 = explode("-", $end_date1[0]);
	$end_hour = explode(":", $end_date1[1]);
	$end_date = date("Y-m-d H:i:s", mktime($end_hour[0], $end_hour[1], $end_hour[2], $end_date2[1], $end_date2[0], $end_date2[2]));
	$title = addslashes($_POST['title']);
	$description = addslashes($_POST['description']);
	$reminder = $_POST['reminder'];
	$sql = "UPDATE calendar SET event_date = '$end_date', title = '$title', description = '$description', reminder = $reminder WHERE id = $id";
	$db->execute($sql, "none");
}

$day = preg_replace("/[^a-zA-Z0-9\s]/", "", $_GET['day']);
$start_date = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), $day, date("Y")));
$end_date = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), $day, date("Y")));
$sql = "SELECT * FROM calendar WHERE event_date BETWEEN '$start_date' AND '$end_date'";
$events = $db->execute($sql, "all");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PIE EDUCATION | Calendar Event View</title>
<link rel="stylesheet" type="text/css" href="/css/style-student.css" />
<link href="/css/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript" src="/js/index.js"></script>
<script language="JavaScript" src="/js/ts_picker.js"></script>
<script language="Javascript1.2">
<!--
setTimeout('location.href="/logout.php?expire=1"',600000);
// -->
</script>
<script type="text/javascript">
$(document).ready(function(){ 
	
	$("#accordion").accordion({ autoHeight: false });

});	
</script>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-35230372-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#000000"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td width="350" height="30">&nbsp;</td>
        <td align="right" valign="bottom"><a href="class.php" class="top-menu" style="color:#999">MY CLASS</a> <a href="schedule.php" class="top-menu" style="color:#999">CALENDAR</a> <a href="homework.php" class="top-menu" style="color:#999">HOMEWORK</a> <a href="test.php" class="top-menu" style="color:#999">EXAMS</a> <a href="grades.php" class="top-menu" style="color:#999">GRADES</a> <a href="tools.php" class="top-menu" style="color:#999">TOOLS</a> <a href="/logout.php" class="top-menu" style="color:#999">SIGN OUT</a></td>
      </tr>
	  <tr>
        <td>&nbsp;</td>
        <td align="right" valign="bottom"><a href="/profile/" class="login-menu" style="color:#FFF"><?php echo $_SESSION['name'].' '.$_SESSION['lname']; ?></a> <a href="/common/redirect.php" class="login-menu" style="color:#FFF">My Documents</a> <a href="/network/" class="login-menu" style="color:#FFF">My Wall</a> <a href="/common/settings.php" class="login-menu" style="color:#FFF">Settings</a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="30" style="background-image:url(/images/top-bg-student.png); background-repeat:repeat-x">&nbsp;</td>
  </tr>
  <tr>
    <td height="550" valign="top"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td>
        
        <div id="accordion">
            <?php
			foreach($events as $event){
			$end_date1 = explode(" ",  preg_replace("/[^a-zA-Z0-9_\-:\.\s]/", "", $event['event_date']));
			$end_date2 = explode("-", $end_date1[0]);
			$end_hour = explode(":", $end_date1[1]);
			$end_date = date("d-m-Y H:i:s", mktime($end_hour[0], $end_hour[1], $end_hour[2], $end_date2[1], $end_date2[2], $end_date2[0]));
            echo '<h3><a href="#">'.$event['title'].'</a></h3>
            <div>
                <form id="form'.$event['id'].'" name="form'.$event['id'].'" method="post" action="schedule-view.php?day='.$day.'" onsubmit="return editEventValidation(this);">
                <table width="980" border="0" align="center" cellpadding="4" cellspacing="4">
                  <tr>
                    <td colspan="2" style="font-size:14px; color:#999">Yo update the calendar just change the values according your needes, after editing click on &quot;Save My Event&quot; and your done. If you don\'t want to edit your event simply click &quot;Go Back to Calendar&quot; and no change will be saved<input name="id" type="hidden" id="id" value="'.$event['id'].'" /></td>
                    </tr>
                  <tr>
                    <td width="200"><h2>Date <span style="color:#F00">*</span></h2></td>
                    <td><input name="end_date" type="text" class="form-course" id="end_date" tabindex="1" value="'.$end_date.'" />  <a href="javascript:show_calendar(\'document.form'.$event['id'].'.end_date\', document.form'.$event['id'].'.end_date.value);"> <img src="/images/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the timestamp" /></a></td>
                  </tr>
                  <tr>
                    <td><h2>Title <span style="color:#F00">*</span></h2></td>
                    <td><input name="title" type="text" class="form-course" id="title" tabindex="2" value="'.$event['title'].'" /></td>
                  </tr>
                  <tr>
                    <td><h2>Description <span style="color:#F00">*</span></h2></td>
                    <td><textarea name="description" class="form-course" id="description" style="width: 700px; height: 120px;" tabindex="3">'.$event['description'].'</textarea></td>
                  </tr>
                  <tr>
                    <td><h2>Reminder <span style="color:#F00">*</span></h2></td>
                    <td><select name="reminder" class="form-course" id="reminder" tabindex="4">
                      <option value="0"'; if($event['reminder'] == 0){ echo ' selected="selected"'; }  echo '>No Reminder</option>
                      <option value="1"'; if($event['reminder'] == 1){ echo ' selected="selected"'; }  echo '>15 Minutes Before</option>
                      <option value="2"'; if($event['reminder'] == 2){ echo ' selected="selected"'; }  echo '>30 Minutes Before</option>
                      <option value="3"'; if($event['reminder'] == 3){ echo ' selected="selected"'; }  echo '>1 Hour Before</option>
                      <option value="4"'; if($event['reminder'] == 4){ echo ' selected="selected"'; }  echo '>1 Day Before</option>
                    </select></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td style="color:#F00; font-family:Arial, Helvetica, sans-serif"><em>all the fields marked with * are required fields</em></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td><input name="send" type="submit" class="submit" id="send" value="Create New Event" /> <input type=button class="submit" onClick="location.href=\'schedule.php\'" value=\'Cancel Creation\' tabindex="10" /></td>
                  </tr>
                </table>
              </form>
            </div>';
			}
            ?>
            
        </div></td>
      </tr>
</table></td>
      </tr>
  <tr>
    <td height="30" align="center" style=" background-image:url(../images/bottom-bg-student.png); background-repeat:repeat-x;">&nbsp;</td>
  </tr>
  <tr>
    <td height="50" align="center" bgcolor="#000000" style="color:#fff; font-family:Arial, Helvetica, sans-serif">PIE.EDU All Rights Reserved 2012<br />
      <a href="/common/privacy.php" class="bottom-menu" style="color:#999">Privacy Policy</a> | <a href="/common/terms.php" class="bottom-menu"  style="color:#999">Terms of Use</a> | <a href="/help/main.php" class="bottom-menu" style="color:#999">Help Center</a> | <a href="/common/jobs.php" class="bottom-menu" style="color:#999">Jobs</a></td>
  </tr>
    </table>
</body>
</html>